﻿using System;
using System.Linq;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class ContractsForm : Form
    {
        int regID, avpID;
        tb_avpstate avpContracts;

        public ContractsForm()
        {
            InitializeComponent();
        }

        private void ContractsForm_Load(object sender, EventArgs e)
        {
            cbRegions.SelectedIndexChanged -= cbRegions_SelectedIndexChanged;
            cbRegions.DataSource = UtilClass.FillCBRegions();
            cbRegions.DisplayMember = "Name";
            cbRegions.ValueMember = "ID";            

            if (InformationClass.isAdmin)
            {                                
                cbRegions.SelectedIndex = -1;
                cbRegions.Text = string.Empty;
                cbRegions.SelectedIndexChanged += new EventHandler(cbRegions_SelectedIndexChanged);
            }
            else
            {                
                cbRegions.Enabled = false;                
                regID = InformationClass.regionID;
                cbRegions.SelectedIndexChanged += new EventHandler(cbRegions_SelectedIndexChanged);
                cbRegions.SelectedValue = regID;
                cbRegions_SelectedIndexChanged(cbRegions, null);
                cbAVP.Enabled = true;
                cbAVP.Text = string.Empty;
                toolStrip1.Visible = false;
            }
        }

        private void cbRegions_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbAVP.SelectedIndexChanged -= cbAVP_SelectedIndexChanged;
            regID = Convert.ToInt32(cbRegions.SelectedValue);
            cbAVP.Enabled = true;
            cbAVP.DataSource = UtilClass.FillAVP(regID);
            cbAVP.DisplayMember = "Name";
            cbAVP.ValueMember = "ID";
            cbAVP.SelectedIndex = -1;
            cbAVP.Text = string.Empty;
            numFarmContracts.Value = numOGMEContracts.Value = numOtherContracts.Value = numUISContracts.Value = 0;
            cbAVP.SelectedIndexChanged += new EventHandler(cbAVP_SelectedIndexChanged);
        }

        private void cbAVP_SelectedIndexChanged(object sender, EventArgs e)
        {
            avpID = Convert.ToInt32(cbAVP.SelectedValue);
            if (avpID < 1)
            {
                MessageBox.Show("Не выбранно АВП");
                return;
            }

            avpContracts = DBClass.AVPState.Where(wh => wh.AVPID == avpID && wh.Year == DateTime.Today.Year).FirstOrDefault();
            if (avpContracts != null)
            {
                groupBox1.Enabled = butAddContract.Enabled = butDelContract.Enabled = true;
                numFarmContracts.Value = (decimal)avpContracts.FHContracts;
                numOGMEContracts.Value = (decimal)avpContracts.OGMEContracts;
                numOtherContracts.Value = (decimal)avpContracts.OtherContrats;
                numUISContracts.Value = (decimal)avpContracts.UISContracts;
            }
            else
            {
                if (MessageBox.Show("Перенести состояние АВП в " + DateTime.Now.Year.ToString() + " год", "Внимание", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    avpContracts = new tb_avpstate
                    {
                        FHContracts = DBClass.AVPState
                            .Where(wh => wh.AVPID == avpID && wh.Year == DBClass.AVPState.Max(mx => mx.Year))
                            .Select(se => se.FHContracts)
                            .SingleOrDefault(),
                        OGMEContracts = DBClass.AVPState
                            .Where(wh => wh.AVPID == avpID && wh.Year == DBClass.AVPState.Max(mx => mx.Year))
                            .Select(se => se.OGMEContracts)
                            .SingleOrDefault(),
                        OtherContrats = DBClass.AVPState
                            .Where(wh => wh.AVPID == avpID && wh.Year == DBClass.AVPState.Max(mx => mx.Year))
                            .Select(se => se.OtherContrats)
                            .SingleOrDefault(),
                        UISContracts = DBClass.AVPState
                            .Where(wh => wh.AVPID == avpID && wh.Year == DBClass.AVPState.Max(mx => mx.Year))
                            .Select(se => se.UISContracts)
                            .SingleOrDefault(),
                        AVPID = avpID,
                        FarmerWaterUsers = DBClass.AVPState
                            .Where(wh => wh.AVPID == avpID && wh.Year == DBClass.AVPState.Max(mx => mx.Year))
                            .Select(se => se.FarmerWaterUsers)
                            .SingleOrDefault(),
                        HasEquipment = DBClass.AVPState
                            .Where(wh => wh.AVPID == avpID && wh.Year == DBClass.AVPState.Max(mx => mx.Year))
                            .Select(se => se.HasEquipment)
                            .SingleOrDefault(),
                        HasFlowJournal = DBClass.AVPState
                            .Where(wh => wh.AVPID == avpID && wh.Year == DBClass.AVPState.Max(mx => mx.Year))
                            .Select(se => se.HasFlowJournal)
                            .SingleOrDefault(),
                        HasLimitPlan = DBClass.AVPState
                            .Where(wh => wh.AVPID == avpID && wh.Year == DBClass.AVPState.Max(mx => mx.Year))
                            .Select(se => se.HasLimitPlan)
                            .SingleOrDefault(),
                        HasOffice = DBClass.AVPState
                            .Where(wh => wh.AVPID == avpID && wh.Year == DBClass.AVPState.Max(mx => mx.Year))
                            .Select(se => se.HasOffice)
                            .SingleOrDefault(),
                        HasUISJournal = DBClass.AVPState
                            .Where(wh => wh.AVPID == avpID && wh.Year == DBClass.AVPState.Max(mx => mx.Year))
                            .Select(se => se.HasUISJournal)
                            .SingleOrDefault(),
                        HasWateringPlan = DBClass.AVPState
                            .Where(wh => wh.AVPID == avpID && wh.Year == DBClass.AVPState.Max(mx => mx.Year))
                            .Select(se => se.HasWateringPlan)
                            .SingleOrDefault(),
                        OtherWaterUsers = DBClass.AVPState
                            .Where(wh => wh.AVPID == avpID && wh.Year == DBClass.AVPState.Max(mx => mx.Year))
                            .Select(se => se.OtherWaterUsers)
                            .SingleOrDefault(),
                        StaffQuantity = DBClass.AVPState
                            .Where(wh => wh.AVPID == avpID && wh.Year == DBClass.AVPState.Max(mx => mx.Year))
                            .Select(se => se.StaffQuantity)
                            .SingleOrDefault(),
                        WaterGrQuantity = DBClass.AVPState
                            .Where(wh => wh.AVPID == avpID && wh.Year == DBClass.AVPState.Max(mx => mx.Year))
                            .Select(se => se.WaterGrQuantity)
                            .SingleOrDefault(),
                        Year = DateTime.Now.Year,

                    };
                    DBClass.AVPState.InsertOnSubmit(avpContracts);
                    try
                    {
                        DBClass.RESPContext.SubmitChanges();
                        MessageBox.Show(msgClass.msgSubmitChanges, msgClass.msgAchtung);
                    }
                    catch
                    {
                        MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
                    }
                }
            }
        }

        private void butAddContract_Click(object sender, EventArgs e)
        {
            if (rbUISContract.Checked)
                avpContracts.UISContracts++;
            if (rbOtherCintract.Checked)
                avpContracts.OtherContrats++;
            if (rbOGMEContract.Checked)
                avpContracts.OGMEContracts++;
            if (rbFarmer.Checked)
                avpContracts.FHContracts++;

            DBClass.RESPContext.SubmitChanges();
            ShowValues();
        }


        private void butDelContract_Click(object sender, EventArgs e)
        {
            if (rbUISContract.Checked)
                avpContracts.UISContracts--;
            if (rbOtherCintract.Checked)
                avpContracts.OtherContrats--;
            if (rbOGMEContract.Checked)
                avpContracts.OGMEContracts--;
            if (rbFarmer.Checked)
                avpContracts.FHContracts--;

            DBClass.RESPContext.SubmitChanges();
            ShowValues();
        }

        private void ShowValues()
        {
            numFarmContracts.Value = (decimal)avpContracts.FHContracts;
            numOGMEContracts.Value = (decimal)avpContracts.OGMEContracts;
            numOtherContracts.Value = (decimal)avpContracts.OtherContrats;
            numUISContracts.Value = (decimal)avpContracts.UISContracts;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (avpContracts != null)
            {
                avpContracts.FHContracts = (int)numFarmContracts.Value;
                avpContracts.OGMEContracts = (int)numOGMEContracts.Value;
                avpContracts.OtherContrats = (int)numOtherContracts.Value;
                avpContracts.UISContracts = (int)numUISContracts.Value;
            }
            DBClass.RESPContext.SubmitChanges();
            if (MessageBox.Show(msgClass.msgSubmitChanges + " Продолжить?", msgClass.msgAchtung, MessageBoxButtons.YesNo) == DialogResult.No)
            {
                this.Close();
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void numUISContracts_ValueChanged(object sender, EventArgs e)
        {
            if (!numUISContracts.Enabled)
                return;
        }

        private void numOGMEContracts_ValueChanged(object sender, EventArgs e)
        {
            if (!numOGMEContracts.Enabled)
                return;
        }

        private void numFarmContracts_ValueChanged(object sender, EventArgs e)
        {
            if (!numFarmContracts.Enabled)
                return;
        }

        private void numOtherContracts_ValueChanged(object sender, EventArgs e)
        {
            if (!numOtherContracts.Enabled)
                return;
        }

        private void tsbEdit_Click(object sender, EventArgs e)
        {
            numFarmContracts.Enabled = numOGMEContracts.Enabled = numOtherContracts.Enabled = numUISContracts.Enabled = true;
        }

    }
}
